<template>
  <div class="login-container">
    <div class="login-wraper">
      <div class="login_ads">
        &copy; 北京安之谷科技有限公司
      </div>
      <div class="login_form_wraper">
          <h3 style="line-height: 50px; height:50px">用户登录</h3>
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
          label-position="left">
          <el-form-item prop="username">
            <el-input ref="username" prefix-icon="el-icon-user" v-model="loginForm.username" placeholder="账号名" name="username" type="text"
              tabindex="1" auto-complete="on" clearable />
          </el-form-item>

          <el-form-item prop="password">
            <el-input :key="passwordType" prefix-icon="el-icon-lock" ref="password" v-model="loginForm.password" :type="passwordType"
              placeholder="密码" name="password" show-password tabindex="2" auto-complete="on"
              @keyup.enter.native="handleLogin" />
          </el-form-item>

          <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;"
            @click.native.prevent="handleLogin">立即登录</el-button>

          <div style="display: flex;">
            <div style="flex:1;text-align: left;">忘记密码</div>
            <div style="flex:1;text-align: right;">注册账号</div>
          </div>
        </el-form>
      </div>
    </div>

  </div>
</template>

<script>
  import {
    validUsername
  } from '@/utils/validate'

  export default {
    name: 'Login',
    data() {
      const validateUsername = (rule, value, callback) => {
        if (!validUsername(value)) {
          callback(new Error('请输入正确账号'))
        } else {
          callback()
        }
      }
      const validatePassword = (rule, value, callback) => {
        if (value.length < 6) {
          callback(new Error('密码长度不小于6位'))
        } else {
          callback()
        }
      }
      return {
        loginForm: {
          username: 'admin',
          password: '111111'
        },
        loginRules: {
          username: [{
            required: true,
            trigger: 'blur',
            validator: validateUsername
          }],
          password: [{
            required: true,
            trigger: 'blur',
            validator: validatePassword
          }]
        },
        loading: false,
        passwordType: 'password',
        redirect: undefined
      }
    },
    watch: {
      $route: {
        handler: function(route) {
          this.redirect = route.query && route.query.redirect
        },
        immediate: true
      }
    },
    methods: {
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true
            this.$store.dispatch('user/login', this.loginForm).then(() => {
              this.$router.push({
                path: this.redirect || '/'
              })
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .login-wraper {
    background-color: #f5f5f5;
    display: flex;
    align-items: center;
    position: relative;
    width: 860px;
    height: 460px;
    background-image: url('~@/assets/login.png');
    background-size: 100% 100%;
    border-radius: 6px;
    box-shadow: 2px 2px 12px 0 rgba(0, 0, 0, .1)
  }

  .login-container {
    background-color: #f5f5f5;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .login_ads {
    flex: 1;
    height: 100%;
    padding: 20px;
    color: #666;
  }

  .login_form_wraper {
    width: 320px;
    padding: 20px;
    margin: 0px auto;
    height:100%;
    text-align: center;
    background-color: #FFF;
  }
</style>
